package com.cu.employmentmanagementsystem.service.impl;

import com.cu.employmentmanagementsystem.dao.*;
import com.cu.employmentmanagementsystem.dto.StudentRecruitmentDTO;
import com.cu.employmentmanagementsystem.entity.*;
import com.cu.employmentmanagementsystem.service.StudentInformationService;
import com.cu.employmentmanagementsystem.service.UserService;
import com.cu.employmentmanagementsystem.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 学生个人信息表(StudentInformation)表服务实现类
 *
 * @author makejava
 * @since 2024-07-01 10:52:17
 */
@Service
public class StudentInformationServiceImpl implements StudentInformationService {
    @Resource
    private StudentInformationDao studentInformationDao;

    @Resource
    private UserService userService;

    @Resource
    private StudentRecruitmentDao studentRecruitmentDao;

    @Resource
    private RecruitInformationDao recruitInformationDao;

    @Resource
    private StudentVitaDao studentVitaDao;

    @Resource
    private CompanyDao companyDao;

    /**
     * 通过ID查询单条数据
     *
     * @return 实例对象
     */
    @Override
    public StudentInformation getInfo() {
        Long studentId = CommonUtils.getStudentId();
        return this.studentInformationDao.queryById(studentId);
    }

    /**
     * 修改数据
     *
     * @param studentInformation 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean update(StudentInformation studentInformation) {
        Long studentId = CommonUtils.getStudentId();
        studentInformation.setId(studentId);
        return this.studentInformationDao.update(studentInformation) > 0;
    }

    @Override
    public List<StudentInformation> queryAllByCondition(StudentInformation studentInformation)
    {
        return this.studentInformationDao.queryAllByCondition(studentInformation);
    }

    /**
     * 获取当前登录学生的简历投递状态
     *
     * @return 返回当前登录学生的简历投递状态对象（StudentRecruitment类型）
     * @throws RuntimeException 如果在获取用户ID或查询数据库时发生异常，将抛出运行时异常
     */
    @Override
    public List<StudentRecruitmentDTO> getResumeStatus() {
        // 获取当前登录用户的UserID
        Long student_id = CommonUtils.getStudentId();

        // 根据用户UserID查询学生的个人简介信息
        StudentVita studentVita = this.studentVitaDao.selectByStudentId(student_id);

        // 检查studentVita是否为null，如果是，则可能表示该用户没有个人简介信息
        // 在实际代码中，你可能需要添加相应的错误处理逻辑
        if (studentVita == null) {
            // 抛出异常或返回null，取决于你的业务逻辑
            throw new RuntimeException("没有找到对应的学生个人简介信息");
        }
        List<StudentRecruitment> studentRecruitmentList = studentRecruitmentDao.queryByresumeId(studentVita.getId());
        List<StudentRecruitmentDTO> studentRecruitmentDTOList = studentRecruitmentList.stream().map(item->
        {
            StudentRecruitmentDTO studentRecruitmentDTO = new StudentRecruitmentDTO();
            Long recruitmentId = item.getRecruitmentId();
            RecruitInformation recruitInformation = recruitInformationDao.queryById(recruitmentId);
            Long companyId = recruitInformation.getCompanyId();
            Company company = companyDao.queryByCompanyId(companyId);
            studentRecruitmentDTO.setCompanyName(company.getName());
            studentRecruitmentDTO.setRecruitInformation(recruitInformation);
            studentRecruitmentDTO.setId(item.getId());
            studentRecruitmentDTO.setDeliveryTime(item.getDeliveryTime());
            studentRecruitmentDTO.setDeliveryStatus(item.getDeliveryStatus());
            return studentRecruitmentDTO;
        }).collect(Collectors.toList());

        // 根据学生个人简介的ID查询简历投递状态
        return studentRecruitmentDTOList;
    }

}
